#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include "mypushbutton.h"
#include "coin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSound>


PlayScene::PlayScene(int index)
{
    QSound * backSound = new QSound(":/resource/BackButtonSound.wav",this);
    QSound * flipcoin = new QSound(":/resource/ConFlipSound.wav",this);
    QSound * winSound = new QSound(":/resource/winsound.wav",this);



    this->levelIndex=index;
    qDebug()<<"您进入的是第"<<this->levelIndex<<"关";

    this->setFixedSize(320,588);//设置窗口尺寸
    this->setWindowTitle("翻金币场景~");//设置标题
    this->setWindowIcon(QIcon(":/resource/Coin0001.png")); //设置图标

    //创建菜单栏
    QMenuBar * bar = menuBar();
    this->setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始"); //创建菜单
    QAction * quitAction = startMenu->addAction("退出");//创建菜单“退出”项；
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });//监听退出


    //返回按钮
       MyPushButton * backBtn = new MyPushButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
       backBtn->setParent(this);
       backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

       connect(backBtn,&MyPushButton::clicked,[=](){
           backSound->play();
        //qDebug()<<"RETURN";

       QTimer::singleShot(500,this,[=](){
            emit this->ChooseSceneBack();//延时 发送点击返回的signals
        });
      });

       //显示当前关卡号
       QLabel * label = new QLabel;
       label->setParent(this);
       label->setText("Level："+QString::number(levelIndex));
       QFont font("华文新魏",20);
       label->setFont(font);
       //设置label的大小和位置
       label->setFixedSize(backBtn->width(),backBtn->height());
       label->move(30,this->height()-50);
       label->setGeometry(QRect(30,this->height()-50,this->width(),50));
       //设置属性
       label->setAttribute(Qt::WA_TransparentForMouseEvents);

       //胜利图片
       //创建胜利图片
       QLabel*winLabel = new QLabel;
       winLabel->setParent(this);
       QPixmap pix;
       pix.load(":/resource/LevelCompletedDialogBg.png");
       winLabel->setPixmap(pix);
       winLabel->setGeometry(0,0,pix.width(),pix.height());
       winLabel->move(this->width()*0.5-pix.width()*0.5,-pix.height());

       //初始化关卡的二维数组
      dataConfig config;
       for(int i=0;i<4;i++){
           for(int j=0;j<4;j++){
               this->game[i][j]=config.mData[this->levelIndex][i][j];
           }
       }

       //创建金币的背景图片
        for(int i=0;i<4;i++){
           for(int j=0;j<4;j++){
               QLabel * label = new QLabel;
               QPixmap pix;
               pix.load(":/resource/BoardNode.png");
               label->setGeometry(0,0,pix.width(),pix.height());
               label->setPixmap(pix);
               label->setParent(this);
               label->move(57+i*pix.width(),200+j*pix.height());

               //创建金币
               QString str;
               if(this->game[i][j]==1){
                   str=":/resource/Coin0001.png";
               }
               else{
                   str=":/resource/Coin0008.png";
               }
               Coin * coin = new Coin(str);
               coin->setParent(this);
               coin->move(59+i*pix.width(),204+j*pix.height());
               coin->posX=i;
               coin->posY=j;
               coin->flag=game[i][j];//硬币状态初始化

               this->coinBtn[i][j]=coin;

               //调用点击硬币函数
               connect(coin,&Coin::clicked,[=](){
                   flipcoin->play();

                   //禁用所有硬币点击
                   for(int i=0;i<4;i++){
                       for(int j=0;j<4;j++){

                           this->coinBtn[i][j]->isWin = true;
                       }
                   }

                   coin->changeFlag();
                   game[i][j]=game[i][j]==0?1:0;//同步二维数组 改变矩阵中的值

                   //延时翻转周围硬币
                   QTimer::singleShot(300,this,[=](){
                       //检测右侧硬币的翻转
                       if(coin->posX+1 <=3){
                           this->coinBtn[coin->posX+1][coin->posY]->changeFlag();
                           game[coin->posX+1][coin->posY]= game[coin->posX+1][coin->posY]==0?1:0;
                       }
                       //检测左侧硬币的翻转
                       if(coin->posX-1 >=0){
                           this->coinBtn[coin->posX-1][coin->posY]->changeFlag();
                           game[coin->posX-1][coin->posY]=game[coin->posX-1][coin->posY]==0?1:0;
                       }
                       //上侧
                       if(coin->posY+1 <=3){
                           this->coinBtn[coin->posX][coin->posY+1]->changeFlag();;
                           game[coin->posX][coin->posY+1]=game[coin->posX][coin->posY+1]==0?1:0;
                       }
                       //下侧
                       if(coin->posY-1 >=0){
                           this->coinBtn[coin->posX][coin->posY-1]->changeFlag();
                           game[coin->posX][coin->posY-1]=game[coin->posX][coin->posY-1]==0?1:0;
                       }

                       //检测游戏是否胜利
                       this->isWin = true;
                       for(int i=0;i<4;i++){
                           for(int j=0;j<4;j++){
                               if(this->coinBtn[i][j]->flag == false ){
                                   this->isWin = false;
                                   break;
                               }
                           }
                       }
                       if(this->isWin){
                           qDebug()<<"游戏胜利";

                           winSound->play();

                           //将所有硬币的胜利标志改为真
                           for(int i=0;i<4;i++){
                               for(int j=0;j<4;j++){
                                   this->coinBtn[i][j]->isWin = true;
                               }
                           }
                           //显示胜利图片
                           QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                           animation->setDuration(1000);
                           animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                           animation->setEndValue(QRect(winLabel->x(),winLabel->y()+170,winLabel->width(),winLabel->height()));
                           animation->setEasingCurve(QEasingCurve::OutBounce);
                           animation->start(QAbstractAnimation::DeleteWhenStopped);

                       }
                       else{
                           for(int i=0;i<4;i++){
                               for(int j=0;j<4;j++){
                                   this->coinBtn[i][j]->isWin = false;
                               }
                           }
                       }


                   });
               });

                     }





           }
       }





void PlayScene::paintEvent(QPaintEvent *)//第三个场景
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/yeahtwo.jpg");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/resource/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}


